package com.ygj.employee.action;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.persistent.model.Employee;
import com.base.common.persistent.model.Terminal;
import com.base.common.utils.EasyUtil;
import com.cjy.common.bean.OpAttributes;
import com.cjy.user.service.UserService;
import com.ygj.employee.service.EmployeeService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import javax.annotation.Resource;
import java.util.List;

import static com.base.common.GlobaConstants.*;

@Action("employeeAction")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class EmployeeAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	private int bid = 0; // 0,android城际云; 1,android城管家; 2,微信城际云; 3,ios城际云;
							// 4,ios云管家

	private int captcha; // 验证码

	private String code; // 设备码

	private int compoundsId; // 小区id

	private int dept; // 职位信息

	@Resource
	private EmployeeService employeeService;

	private String phone; // 手机号

	@Resource
	private UserService userService;

	/**
	 * 员工选择默认小区
	 * 
	 * @return
	 */
	public String chooseDefaultCompounds(){
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", employeeService.chooseDefaultCompounds(((Employee)session.get("users")).getId(), compoundsId)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 0));
		}
		return returnType();
	}

	/**
	 * 云管家登录
	 * 
	 * @return
	 */
	public String employeeLogin() {
		try {
			Terminal terminal = new Terminal();

			if (bid == 4) {
				terminal.setAppleId(code);
			} else {
				terminal.setCode(code);
			}
			terminal.setBid(bid);
			List employees = userService.login(terminal);
			if (employees.size() > 0) {
				session.put("users", employees);
				actionResult = ActionResult.getActionResult(new OpAttributes()
						.add("code", 1).add("result", employees));
			} else {
				actionResult = ActionResult.getActionResult(new OpAttributes()
						.add("code", 0));
			}
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 云管家注册
	 * 
	 * @return
	 */
	public String employeeRegiste() {
		try {
			Terminal terminal = new Terminal();

			if (bid == 1) {
				terminal.setPhone(EasyUtil.rc4(phone, "szcjy").trim());
				terminal.setCode(code.trim());
			} else if (bid == 4) {
				terminal.setPhone(phone.trim());
				terminal.setAppleId(code.trim());
			}
			terminal.setBid(bid);
			int result = userService.managerRegiste(terminal);
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", result));
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	public int getBid() {
		return bid;
	}

	public int getCaptcha() {
		return captcha;
	}

	public String getCode() {
		return code;
	}

	public int getCompoundsId() {
		return compoundsId;
	}

	public int getDept() {
		return dept;
	}

	/**
	 * 根据部门职位获取员工列表
	 * 
	 * @return
	 */
	public String getEmployeesByDept() {
		try {
			actionResult = ActionResult
					.getActionResult(new OpAttributes()
							.add("code", 1)
							.add("result",
									employeeService
											.getEmployeesByDeptAndCompoundsId(dept, compoundsId)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	public String getPhone() {
		return phone;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	public void setCaptcha(int captcha) {
		this.captcha = captcha;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public void setCompoundsId(int compoundsId) {
		this.compoundsId = compoundsId;
	}

	public void setDept(int dept) {
		this.dept = dept;
	}
	
	public void setPhone(String phone) {
		this.phone = phone;
	}

}
